package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ShouldGatherPayCount.GatherPayCount;

/**
 * 应收、付款统计详情
 *
 * @author wu
 */
public class ShouldCountDetailFragment extends Fragment {

  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ShouldCountDetailFragment.EXTRA_VALUE";
  public static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.ShouldCountDetailFragment.EXTRA_TYPE";
  private TextView mtvContractDeliveryCount;// 合同订单时间
  private TextView mtvUnitCount;// 客户单位/往来单位
  private TextView mtvCustomerCount;// 客户单位/往来单位
  private TextView mtvTypeCount;// 类型
  private TextView mtvHappenedCostCount;// 发生金额

  private TextView mtvHasCalculateDetailCount;// 已结算/已支付
  private TextView mtvNotCalculateDetailCount;// 未结算/未支付
  private TextView mtvHasOpenBillingCount;// 已开票/已收票
  private TextView mtvNoOpenBillingCount;// 未开票/未收票
  private LinearLayout mllContractDeliveryTime;
  private LinearLayout mllCompanyUnit;
  private LinearLayout mllType;

  private TextView mtvHasCount;// 已结算/已支付
  private TextView mtvNotCount;// 未结算/未支付
  private TextView mtvHasOpenBill;// 已开票/已收票
  private TextView mtvNotOpenBill;// 未开票/未收票
  private String mType;
  private GatherPayCount mCount;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mCount = (GatherPayCount) getArguments().getSerializable(EXTRA_VALUE);
    mType = getArguments().getString(EXTRA_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_count_detail, container, false);
    mtvContractDeliveryCount = (TextView) view.findViewById(R.id.tv_contractDelivery_count);
    mtvUnitCount = (TextView) view.findViewById(R.id.tv_unitCount);
    mtvCustomerCount = (TextView) view.findViewById(R.id.tv_customer_count);
    mtvTypeCount = (TextView) view.findViewById(R.id.tv_type_count);
    mtvHappenedCostCount = (TextView) view.findViewById(R.id.tv_happenedCost_count);
    mtvHasCount = (TextView) view.findViewById(R.id.tv_hasCount);
    mtvHasCalculateDetailCount = (TextView) view.findViewById(R.id.tv_hasCalculateDetail_count);
    mtvNotCount = (TextView) view.findViewById(R.id.tv_notCount);
    mtvNotCalculateDetailCount = (TextView) view.findViewById(R.id.tv_notCalculateDetail_count);
    mtvHasOpenBill = (TextView) view.findViewById(R.id.tv_hasOpenBill);
    mtvHasOpenBillingCount = (TextView) view.findViewById(R.id.tv_hasOpenBilling_count);
    mtvNotOpenBill = (TextView) view.findViewById(R.id.tv_notOpenBill);
    mtvNoOpenBillingCount = (TextView) view.findViewById(R.id.tv_noOpenBilling_count);

    mllContractDeliveryTime = (LinearLayout) view.findViewById(R.id.ll_contractDeliveryTime);
    mllCompanyUnit = (LinearLayout) view.findViewById(R.id.ll_companyUnit);
    mllType = (LinearLayout) view.findViewById(R.id.ll_type);
    initData();
    return view;
  }

  private void initData() {
    if (ShouldCountListFragment.VALUE_GATHER.equalsIgnoreCase(mType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.getCountDetail);
      mtvUnitCount.setText(R.string.customerEnter);
      mtvHasCount.setText(R.string.hasCalculateDetail);
      mtvNotCount.setText(R.string.notCalculateDetail);
      mtvHasOpenBill.setText(R.string.hasOpenBilling);
      mtvNotOpenBill.setText(R.string.noOpenBilling);
    } else if (ShouldCountListFragment.VALUE_PAY.equalsIgnoreCase(mType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.payCountDetail);
      mtvUnitCount.setText(R.string.communicateUnit);
      mtvHasCount.setText(R.string.hasPayDetail);
      mtvNotCount.setText(R.string.notPayDetail);
      mtvHasOpenBill.setText(R.string.hasacceptBilling);
      mtvNotOpenBill.setText(R.string.noacceptBilling);
    }
    if (!TextUtils.isEmpty(mCount.getOtherHelp())) {
      mtvContractDeliveryCount.setText(mCount.getOtherHelp());
    } else {
      mllContractDeliveryTime.setVisibility(View.GONE);
    }
    if (!TextUtils.isEmpty(mCount.getPartName())) {
      mtvCustomerCount.setText(mCount.getPartName());
    } else {
      mllCompanyUnit.setVisibility(View.GONE);
    }
    if (!TextUtils.isEmpty(mCount.getContractKindName())) {
      mtvTypeCount.setText(mCount.getContractKindName());
    } else {
      mllType.setVisibility(View.GONE);
    }
    if (!TextUtils.isEmpty(mCount.getContractAmount())) {
      mtvHappenedCostCount.setText(mCount.getContractAmount());
    }
    if (!TextUtils.isEmpty(mCount.getMakedContractAmount())) {
      mtvHasCalculateDetailCount.setText(mCount.getMakedContractAmount());
    }
    if (!TextUtils.isEmpty(mCount.getResidualContractAmount())) {
      mtvNotCalculateDetailCount.setText(mCount.getResidualContractAmount());
    }
    if (!TextUtils.isEmpty(mCount.getMakedInvoiceAmount())) {
      mtvHasOpenBillingCount.setText(mCount.getMakedInvoiceAmount());
    }
    if (!TextUtils.isEmpty(mCount.getResidualInvoiceAmount())) {
      mtvNoOpenBillingCount.setText(mCount.getResidualInvoiceAmount());
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(GatherPayCount count, String type) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, count);
    bundle.putString(EXTRA_TYPE, type);
    ShouldCountDetailFragment fragment = new ShouldCountDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
